package org.bikelab.se3.index;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.bikelab.se3.index.siren.SirenLuceneWriter;
import org.bikelab.se3.index.siren.SirenSolrWriter;
import org.bikelab.se3.rdf.reader.NoLineToParseException;
import org.bikelab.se3.rdf.reader.RdfReaderFactory;
import org.bikelab.se3.rdf.reader.WrongNumberOfSegmentsException;
import org.semanticweb.yars.nx.Node;

public class Se3Indexer extends Configured implements Tool {
	public static class Map extends Mapper<LongWritable, Text, Text, Text> {
		public void map(LongWritable key, Text value, Context context)
				throws IOException {
			try {
				Node[] quard = RdfReaderFactory.parseNqLine(value.toString());

				String triple = quard[0].toN3() + " " + quard[1].toN3() + " " + quard[2].toN3() + " .";
				
				context.write(new Text(quard[3].toN3()), new Text(triple));
			} catch (WrongNumberOfSegmentsException e) {
				e.printStackTrace();
			} catch (NoLineToParseException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static class Reduce extends Reducer<Text, Text, Text, Text> {
		public void reduce(Text key, Iterable<Text> values, Context context)
				throws IOException {
			String triples = "";
			Iterator<Text> list = values.iterator();
			while(list.hasNext()) {
				triples += list.next().toString() + "\n";
			}
			
			try {
				context.write(key, new Text(triples));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public int run(String[] args) throws Exception {
		Job job = new Job(getConf());
		job.setJarByClass(Se3Indexer.class);
		job.setJobName("Se3 Indexer");
		job.setMapperClass(Map.class);
		job.setReducerClass(Reduce.class);
		job.setNumReduceTasks(3);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		job.setInputFormatClass(TextInputFormat.class);
		job.setOutputFormatClass(Se3IndexerOutputFormat.class);
//		Se3IndexWriterFactory.addClassToConf(job.getConfiguration(), SirenLuceneWriter.class);
		Se3IndexWriterFactory.addClassToConf(job.getConfiguration(), SirenSolrWriter.class);
		FileInputFormat.setInputPaths(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		boolean success = job.waitForCompletion(true);
		return success ? 0 : 1;
	}

	public static void main(String[] args) throws Exception {
		int res = ToolRunner.run(new Configuration(), new Se3Indexer(), args);
		System.exit(res);
	}

}
